package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.dto.BusinessPoolDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @GetMapping
    public Result getBusinesses(BusinessDto businessDto) {
        log.info("businessDto:{}", businessDto);
        PageResult<Business> pageResult = businessService.selectPage(businessDto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addBusiness(Business business) {
        log.info("business:{}", business);
        business.setStatus(1);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }

    //分配商机
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("businessId:{},userId:{}", businessId, userId);
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }

    //根据id查询商机
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        log.info("id:{}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }

    //跟进商机
    @PutMapping
    public Result updateBusiness(@RequestBody Business business) {
        log.info("business:{}", business);
        businessService.updateBusiness(business);
        return Result.success();
    }

    //踢回公海
    @PutMapping("/back/{id}")
    public Result kickBack(@PathVariable Integer id) {
        log.info("businessId:{}", id);
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        businessService.updateById(business);
        return Result.success();
    }

    //转客户
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("businessId:{}", id);
        businessService.toCustomer(id);
        return Result.success();
    }

    //公海池
    @GetMapping("/pool")
    public Result pool(BusinessPoolDto businessPoolDto) {
        PageResult<Business> pageResult = businessService.listPage(businessPoolDto);
        return Result.success(pageResult);
    }


}
